import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/brn/brn_delete_tag.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/yt_expandable_tree/model/tree_mode.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/pages/site/site_observer_detail/site_observer_detail_controller.dart';
import 'package:yt_devops/pages/site/site_observer_detail/views/expansion_custom.dart';
import 'package:yt_devops/pages/site/site_observer_detail/views/observer_subtitle_widget.dart';
import 'package:yt_devops/pages/site/site_observer_detail/views/site_observer_list_widget.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';

class SiteObserverDetailView extends GetView<SiteObserverDetailController> {
  SiteObserverDetailView({Key? key}) : super(key: key);

  @override
  final controller = Get.put(SiteObserverDetailController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget.defaultAppBar(
          ResKey.siteSettingObserverChooseObserverPoint.tr),
      body: SafeArea(
          child: GetBuilder<SiteObserverDetailController>(
              init: controller,
              builder: (controller) {
                return Column(
                  children: [
                    _titleWidget(
                        '${ResKey.siteSettingObserverSelected.tr}(${controller.tagController.tags.length})'),
                    Container(
                        color: Colors.white,
                        height: controller.deleteContainerHeight,
                        constraints: const BoxConstraints(
                          maxHeight: 180,
                          minHeight: 20,
                        ),
                        child: ListView(
                          padding: EdgeInsets.zero,
                          children: [
                            controller.allSelectedObserver.isNotEmpty
                                ? Container(
                                    color: Colors.white,
                                    child: YtBrnDeleteTag(
                                        key: controller.deleteKey,
                                        padding: EdgeInsets.only(
                                          left: 16.w,
                                          bottom: 10,
                                          right: 16.w,
                                        ),
                                        themeData: BrnTagConfig(
                                            tagHeight: 32,
                                            tagWidth: 154.w,
                                            tagMinWidth: 104.w,
                                            tagRadius: 4),
                                        onTagDelete: (p0, p1, index) {
                                          controller
                                              .allSelectedObserverModelList
                                              .removeAt(index);
                                          controller.allObserverSelectName
                                              .removeAt(index);
                                          controller.deleteSiteTag(index);
                                        },
                                        isLimitWidth: false,
                                        controller: controller.tagController),
                                  )
                                : Container(),
                          ],
                        )),
                    const SizedBox(
                      height: 10,
                    ),
                    _titleWidget(
                        ResKey.siteSettingObserverChooseDeviceObserverPoint.tr),
                    Expanded(
                      // height: 400,
                      // color: Colors.green,
                      child: Container(
                          color: Colors.white,
                          child: Row(
                            children: [
                              Container(
                                width: 225.w,
                                color: Colour.backgroundColor,
                                child: ListView(
                                    children: _buildExpansionWidgets()),
                              ),
                              SizedBox(
                                width: 150.w,
                                // color: Colors.red,
                                child: Column(
                                  children: [
                                    Container(
                                      alignment: Alignment.centerLeft,
                                      margin: EdgeInsets.only(left: 16.w),
                                      height: 40,
                                      child: Text(
                                        ResKey.siteSettingObserverObserverPoint
                                            .tr,

                                        style: ytTextStyle(
                                            fontWeight: FontWeight.w500,
                                            fontSize: 14),
                                      ),
                                    ),
                                    Expanded(
                                        child: SiteObserverListWidget(
                                                controller.observerList,
                                                controller
                                                    .allObserverSelectName,
                                                controller
                                                    .allSelectedObserverModelList,
                                                controller.allSelectedObserver,
                                                (list) {
                                                Log.d(
                                                    'controller.allSelectedObserver==${controller.allSelectedObserver}');

                                                controller.tagController
                                                    .setTags(controller
                                                        .allSelectedObserver);
                                                if (controller.deleteKey
                                                        .currentContext !=
                                                    null) {
                                                  controller
                                                          .deleteContainerHeight =
                                                      controller
                                                          .deleteKey
                                                          .currentContext!
                                                          .size!
                                                          .height;
                                                }
                                                if (list.isEmpty) {
                                                  controller
                                                      .deleteContainerHeight = 20;
                                                }
                                                if (list.length == 1) {
                                                  controller
                                                      .deleteContainerHeight = 65;
                                                }
                                                controller.update();
                                                Future.delayed(
                                                    const Duration(
                                                        milliseconds: 50), () {
                                                  // 延迟执行的代码
                                                  controller
                                                          .deleteContainerHeight =
                                                      controller
                                                          .deleteKey
                                                          .currentContext!
                                                          .size!
                                                          .height;
                                                  controller.update();
                                                });
                                              })
                                            )
                                  ],
                                ),
                              ),
                            ],
                          )),
                    ),

                    /// 执行按钮
                    Container(
                      margin: EdgeInsets.only(left: 16.w, right: 16.w),
                      padding: const EdgeInsets.only(top: 16),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          SizedBox(
                            child: GestureDetector(
                              onTap: () => controller.clearChoose(),
                              child: Column(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Image.asset(
                                    ImageAssets.iconRenew,
                                    width: 20.w,
                                    height: 20.h,
                                  ),
                                  Text(
                                    ResKey.reset.tr,
                                    style: TextStyle(
                                      color: Colour.mainTextColor,
                                      fontSize: 14.sp,
                                      fontWeight: FontWeight.w500,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                          Expanded(
                            child: Container(
                              margin: EdgeInsets.only(
                                  left: 18.w,
                                  bottom: ScreenUtil().bottomBarHeight == 0
                                      ? 10
                                      : 0),
                              child: BrnBigMainButton(
                                title: ResKey.sure.tr,
                                themeData: BrnButtonConfig(),
                                onTap: () => controller.confirm(),
                              ),
                            ),
                          ),
                        ],
                      ),
                    )
                  ],
                );
              })),
    );
  }

  _buildExpansionWidgets() {
    List<Widget> list = [];
    if (controller.treeData.isNotEmpty) {
      for (int i = 0; i < controller.treeData.length; i++) {
        TreeNode map = controller.treeData[i];
        var widget = ExpansionCard(
          title: map.title,
          contentWidget: expandItems(controller.treeData[i]),
        );
        list.add(widget);
      }
    }
    return list;
  }

  List<Widget> expandItems(TreeNode node) {
    List<Widget> list = [];
    if (node.subNodes != null && node.subNodes!.isNotEmpty) {
      for (int i = 0; i < node.subNodes!.length; i++) {
        TreeNode map = node.subNodes![i];
        var widget = ObverserSubtitle(map.title, map.checked, () {
          ///先取消所有选中的条目
          for (int i = 0; i < controller.treeData.length; i++) {
            TreeNode node = controller.treeData[i];
            for (int j = 0; j < node.subNodes!.length; j++) {
              TreeNode map = node.subNodes![j];
              map.checked = 0;
            }
          }

          for (var element in node.subNodes!) {
            if (element.value == map.value) {
              element.checked = 2;
            } else {
              element.checked = 0;
            }
          }
          controller.update();
          controller.getObserverData(map.value.toString(), map.title);
        });
        list.add(widget);
      }
    }
    return list;
  }

  _titleWidget(String title) {
    return Container(
      color: Colors.white,
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.only(left: 16.w),
      height: 50,
      child: Text(
        title,
        style: ytTextStyle(fontWeight: FontWeight.w500, fontSize: 16),
      ),
    );
  }
}
